home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / text / tex / rail.lha / gram.y < prev    next >
Text File  |  1992-09-23  |  3KB  |  227 lines

  1. /* gram.y - yacc grammar for rail program */
  2.  
  3. %{
  4.  
  5. #include <stdio.h>
  6. #ifdef _DCC
  7. #include <alloca.h>
  8. #endif 
  9. #include "rail.h"
  10.  
  11. char optchar;
  12.  
  13. %}
  14.  
  15. /* identifier */
  16.  
  17. %token <id> IDENTIFIER
  18.  
  19. /* number */
  20.  
  21. %token <num> NUMBER
  22.  
  23. /* [annotation] */
  24.  
  25. %token <text> ANNOT
  26.  
  27. /* \rail@i \rail@p \rail@t \\ */
  28.  
  29. %token RAILI RAILP RAILT RAILCR
  30.  
  31. /* TeX control sequence */
  32.  
  33. %token CS
  34.  
  35. /* 'c' "string" */
  36.  
  37. %token <text> STRING
  38.  
  39. %type <rule> rule rules
  40.  
  41. %type <body> body body0 body1 body2e body2 body3 body4e body4 empty
  42.  
  43. %type <text> annot
  44.  
  45. %start rails
  46.  
  47. %%
  48.  
  49. rails    : rails rail
  50.     | rail
  51.     ;
  52.  
  53. rail    : RAILI raili
  54.     | RAILP railp
  55.     | RAILT railt
  56.     | error
  57.     ;
  58.  
  59. railp    :
  60.       '{'
  61.         {
  62.             fprintf(outf,"\\rail@p {");
  63.             copy=1;
  64.             optchar = '-'; }
  65.       options '}'
  66.         {
  67.             fprintf(outf,"\n");
  68.             copy=0;
  69.         }
  70.     ;
  71.  
  72. options    : /*empty*/
  73.     | options '-'
  74.         { optchar = '-'; }
  75.     | options '+'
  76.         { optchar = '+'; }
  77.     | options IDENTIFIER
  78.         {
  79.             if(setopt(optchar,$2->name)==0)
  80.                 error("unknown option",(char *)NULL);
  81.  
  82.             if($2->kind==UNKNOWN)
  83.                 delete($2);
  84.         }
  85.     ;
  86.  
  87. railt    : '{' IDENTIFIER '}'
  88.         {
  89.             if($2->kind==UNKNOWN || $2->kind==TOKEN)
  90.                 $2->kind=TERM;
  91.             else
  92.                 redef($2);
  93.  
  94.             fprintf(outf,"\\rail@t {%s}\n",$2->name);
  95.         }
  96.     ;
  97.  
  98. raili    : '{' NUMBER '}'
  99.         {
  100.             fprintf(outf,"\\rail@i {%d}",$2);
  101.             copy=1;
  102.         }
  103.       '{' rules '}'
  104.         {    copy=0;
  105.             fprintf(outf,"\n");
  106.             fprintf(outf,"\\rail@o {%d}{\n",$2);
  107.             outrule($6);    /* embedded action is $4 */
  108.             freerule($6);
  109.             fprintf(outf,"}\n");
  110.         }
  111.     ;
  112.  
  113. rules    : rules ';' rule
  114.         { $$=addrule($1,$3); }
  115.     | rules ';'
  116.         { $$=$1; }
  117.     | rule
  118.     | error
  119.         { $$=NULL; }
  120.     ;
  121.  
  122. rule    : IDENTIFIER ':'
  123.         { errorid=$1; }
  124.           body
  125.         {
  126.             if($1->kind==UNKNOWN || $1->kind==TOKEN)
  127.                 $1->kind=NTERM;
  128.             else
  129.                 redef($1);
  130.  
  131.              $$=newrule($1,$4);    /* embedded action is $3 */
  132.  
  133.             errorid=NULL;
  134.         }
  135.     | body
  136.         {
  137.             anonymous++;
  138.  
  139.             $$=newrule((IDTYPE *)NULL,$1);
  140.         }
  141.     ;
  142.  
  143. body    : body0
  144.         { $$=$1; $$->done=1; }
  145.     ;
  146.  
  147. body0    : body0 '|' ANNOT body1
  148.         {
  149.             $$=newbody(ANNOTE,NULLBODY,NULLBODY);
  150.             $$->text=$3;
  151.             $$=addbody(CAT,$$,$4);
  152.             $$=addbody(BAR,$1,$$);
  153.         }
  154.     | body0 '|' body1
  155.         { $$=addbody(BAR,$1,$3); }
  156.     | ANNOT body1
  157.         {
  158.             $$=newbody(ANNOTE,NULLBODY,NULLBODY);
  159.             $$->text=$1;
  160.             $$=addbody(CAT,$$,$2);
  161.         }
  162.     | body1
  163.     ;
  164.  
  165. body1    : body2 '*' body4e
  166.         {
  167.             if(altstar && isemptybody($3)) {
  168.                 $$=newbody(EMPTY,NULLBODY,NULLBODY);
  169.                 $$=addbody(PLUS,$$,revbody($1));
  170.             } else {
  171.                 $$=newbody(EMPTY,NULLBODY,NULLBODY);
  172.                 $$=addbody(BAR,$$,addbody(PLUS,$1,revbody($3)));
  173.             }
  174.         }
  175.     | body2 '+' body4e
  176.         { $$=newbody(PLUS,$1,revbody($3)); }
  177.     | body2e
  178.     ;
  179.  
  180. body2e    : body2 | empty ;
  181.  
  182. body2    : body2 body3
  183.         { $$=addbody(CAT,$1,$2); }
  184.     | body3
  185.     ;
  186.  
  187. body3    : body4 '?'
  188.         { $$=addbody(BAR,newbody(EMPTY,NULLBODY,NULLBODY),$1); }
  189.     | body4
  190.     ;
  191.  
  192. body4e    : body4 | empty ;
  193.  
  194. body4    : '(' body0 ')'
  195.         { $$=$2; $$->done=1; }
  196.     | STRING annot
  197.         {
  198.             $$=newbody(STRNG,NULLBODY,NULLBODY);
  199.             $$->annot=$2;
  200.             $$->text=$1;
  201.         }
  202.     | IDENTIFIER annot
  203.         {
  204.             if($1->kind==UNKNOWN)
  205.                 $1->kind=TOKEN;
  206.  
  207.             $$=newbody(IDENT,NULLBODY,NULLBODY);
  208.             $$->annot=$2;
  209.             $$->id=$1;
  210.         }
  211.     | RAILCR
  212.         { $$=newbody(CR,NULLBODY,NULLBODY); }
  213.     ;
  214.  
  215. empty    : /*empty*/
  216.         { $$=newbody(EMPTY,NULLBODY,NULLBODY); }
  217.     ;
  218.  
  219. annot    : ANNOT
  220.         { $$=$1; }
  221.     | /*empty*/
  222.         { $$=NULL; }
  223.     ;
  224.  
  225. %%
  226.  
  227.